package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eServerState;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.web.controller.BaseHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@WebServlet(urlPatterns = "/platformQueryServerList")
public class PlatformQueryServerList extends BaseHttpServlet implements Serializable {

    private static final long serialVersionUID = -6665912671909388238L;

    private static Logger logger = LogManager.getLogger(PlatformQueryServerList.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("request platformQueryServerList");
        long time;
        String cmd;
        String sign;
        try{
            time = Long.parseLong(request.getParameter("time"));
            cmd = request.getParameter("cmd");
            sign = request.getParameter("sign");
        }catch (Exception e){
            logger.error("platformQueryServerList argument error:" + e);
            send37Result(response, 0, "argument error");
            return;
        }
        if(!Objects.equals("serverList", cmd)){
            logger.error("platformQueryServerList argument cmd:{}", cmd);
            send37Result(response, 0, "argument error");
            return;
        }
        List<ServerInfo> serverInfoList = ServerListMgr.getAllServerList();

        JSONObject resultObj = get37ResultObj(1, "success");

        JSONArray serverListArr = new JSONArray();
        for (ServerInfo serverInfo : serverInfoList) {
            JSONObject serverObj = new JSONObject();
            serverObj.put("sid", serverInfo.getServerId());
            serverObj.put("serverName", serverInfo.getServerName());
            serverObj.put("startTime", DateHelper.getCurrentDateTimeString(serverInfo.getOpenTime() * 1000));
            serverObj.put("isTest", serverInfo.getServerType() != ServerListMgr.SERVER_TYPE_NORMAL);
            serverObj.put("isShow", ServerListMgr.isServerOpen(serverInfo));
//            serverObj.put("isMaintain", serverInfo.getState() != eServerState.Normal.getValue());
            serverObj.put("isMaintain", false);                 //接口方说直接传false
            serverListArr.add(serverObj);
        }
        JSONObject listObj = new JSONObject();
        listObj.put("list", serverListArr);
        resultObj.put("data", listObj);

        sendMessage(response, resultObj.toJSONString());
    }
}
